package com.sinevil.tcpTrans.utils.fileTrans.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.SneakyThrows;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;

/**
 * @Classname FileSliceInfo
 * @Description 文件切片信息
 * @Version 1.0.0
 * @Date 2025/1/6 18:22
 * @Created by sinevil
 */
@Data
@Builder
@AllArgsConstructor
public class FileSliceInfo {
    // 从0开始
    protected int sliceIndex;
    protected String sliceMD5;
    protected String fileMD5;
    protected Path path;
    protected RandomAccessFile raf;
    protected FileChannel channel;
    protected int sliceSize; // 切片大小
    protected int writtenByte; // 已写入大小
    protected SocketChannel socketChannel;

    public boolean isComplete(){
        return sliceSize == writtenByte;
    }

    /**
     * 重置
     */
    @SneakyThrows
    public void reset(){
        this.writtenByte = 0;
        raf.setLength(0);
    }

    /**
     * 更新已写入大小
     * @param written 新写入大小
     */
    public void reNewWrittenByte(int written){
        this.writtenByte += written;
    }

    @SneakyThrows
    public void releaseResource(){
        this.raf.close();
    }


}
